package com.ruoyi.cust.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.cust.domain.CityProject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.ruoyi.cust.mapper.CityCollectMapper;
import com.ruoyi.cust.domain.CityCollect;
import com.ruoyi.cust.service.ICityCollectService;

/**
 * 收藏Service业务层处理
 *
 * @author cjq
 * @date 2024-12-06
 */
@Service
public class CityCollectServiceImpl implements ICityCollectService {
    @Autowired
    private CityCollectMapper cityCollectMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询收藏
     *
     * @param collectId 收藏主键
     * @return 收藏
     */
    @Override
    public CityCollect selectCityCollectByCollectId(Long collectId) {
        return cityCollectMapper.selectCityCollectByCollectId(collectId);
    }

    /**
     * 查询收藏列表
     *
     * @param cityCollect 收藏
     * @return 收藏
     */
    @Override
    public List<CityCollect> selectCityCollectList(CityCollect cityCollect) {
        return cityCollectMapper.selectCityCollectList(cityCollect);
    }

    /**
     * 新增收藏
     *
     * @param cityCollect 收藏
     * @return 结果
     */
    @Override
    public int insertCityCollect(CityCollect cityCollect) {
                cityCollect.setCreateTime(DateUtils.getNowDate());
            return cityCollectMapper.insertCityCollect(cityCollect);
    }

    /**
     * 修改收藏
     *
     * @param cityCollect 收藏
     * @return 结果
     */
    @Override
    public int updateCityCollect(CityCollect cityCollect) {
        return cityCollectMapper.updateCityCollect(cityCollect);
    }

    /**
     * 批量删除收藏
     *
     * @param collectIds 需要删除的收藏主键
     * @return 结果
     */
    @Override
    public int deleteCityCollectByCollectIds(Long[] collectIds) {
        return cityCollectMapper.deleteCityCollectByCollectIds(collectIds);
    }

    /**
     * 删除收藏信息
     *
     * @param collectId 收藏主键
     * @return 结果
     */
    @Override
    public int deleteCityCollectByCollectId(Long collectId) {
        return cityCollectMapper.deleteCityCollectByCollectId(collectId);
    }

    /**
     * 查看该项目是否被收藏
     * @param projectId
     * @param userId
     * @return
     */
    @Override
    public Boolean collectHisByProjectId(Long projectId,Long userId){
       int num = cityCollectMapper.collectHisByProjectId(projectId,userId);
       return num!=0;
    }

    @Override
    public List<CityProject> hisProject(Long userId, String searchInfo, Integer pageSize, Integer pageNum){
        int start = (pageNum-1) * pageSize;
        int end = start + pageSize - 1;
        String projectHisKey = "myProjectHis_"+userId;
        List<CityProject> list = redisTemplate.opsForList().range(projectHisKey,start,end);
        if(StringUtils.isBlank(searchInfo)){
            return list;
        }
       return list.stream().filter(i->i.getProjectTitle().contains(searchInfo)).collect(Collectors.toList());
    }
}
